/**
 * This file is part of the FlexiTimer project.
 * 
 * Author: Florian Arndt <f.aarndt@web.de>
 *  	   Clemens Teichmann <>
 *
 * http://code.google.com/p/flexitimer/ 
 */

package flexitimer_client.calc;

import java.awt.event.ActionEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.AbstractAction;

import flexitimer_client.gui.frame;
import flexitimer_client.gui.optionFrame;
import flexitimer_client.calc.autoUpdateAction;

public class updateAction extends AbstractAction {
		
	
	public updateAction() {
		super("Countdown starten");
	}

	@SuppressWarnings("deprecation")
	public void actionPerformed(ActionEvent e) {
		if(autoUpdateAction.getCountdownStatus()){
			    //SimpleDateFormat sdfa = new SimpleDateFormat("hh:mm a");
			    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
			    SimpleDateFormat sdf2 = new SimpleDateFormat("HHmm");
			    SimpleDateFormat sdf3 = new SimpleDateFormat("HH.mm");
			    Date startTime = null;
			    Date workTime = null;
			    int difHour1;
			    int difMin1 = 0;
			    Date maxTime = null;
			    Date aktuell = new Date();
			    
				try {
					startTime = sdf.parse(frame.getInstance().getStartTime());
					//startTime = sdf2.parse(frame.getInstance().getStartTime());
					maxTime = sdf.parse(optionFrame.getInstance().getMaxTime());
					workTime = sdf.parse("00:00");
			    	workTime.setHours(startTime.getHours() + maxTime.getHours());
			    	workTime.setMinutes(startTime.getMinutes() + maxTime.getMinutes());
			    	difHour1 = workTime.getHours() - aktuell.getHours();
					difMin1 = workTime.getMinutes() - aktuell.getMinutes();
					if(difMin1 < 0) {
						difHour1 -= 1;
						difMin1 = 60 + difMin1;
					}
					if (difHour1 < 0) {
						difHour1 = 0;
						difMin1 = 0;
					}
					
					frame.getInstance().addText("Countdown gestartet: "
							+ difHour1 + ":" + String.format("%02d", difMin1));
					
				} catch (ParseException e1) {
					frame.getInstance().addText("ERROR: Zeit falsch eingegeben! (hh:mm)");
				}
			}	
	}
}
